package wb.is.besti.edu.cd.emergency_responsesys.ui;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import wb.is.besti.edu.cd.emergency_responsesys.R;
import wb.is.besti.edu.cd.emergency_responsesys.utils.Constant;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_nickname,et_username,et_pwd;
    private Button btn_confirm,btn_back;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar!=null){
            actionBar.hide();
        }
        initUI();
    }
    private void initUI(){
        et_nickname = (EditText)findViewById(R.id.et_nickname);
        et_username = (EditText)findViewById(R.id.et_r_username);
        et_pwd = (EditText)findViewById(R.id.et_r_pwd);
        btn_confirm = (Button)findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(this);
        btn_back = (Button)findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        String nickname = et_nickname.getText().toString();
        String username = et_username.getText().toString();
        String pwd = et_pwd.getText().toString();
        if (view.getId()==R.id.btn_confirm){
            if (nickname.equals("")||username.equals("")||pwd.equals("")){
                Toast.makeText(this,"任一空不可为空",Toast.LENGTH_SHORT).show();
            }else {
                String registerUrlStr = Constant.URL_REGISTER+ "?username=" + username + "&pwd=" + pwd+"&nickname=" + nickname;
                new MyAsyncTask().execute(registerUrlStr);
                finish();
            }
        }
        if (view.getId()==R.id.btn_back){
            Intent i = new Intent(RegisterActivity.this,LoginActivity.class);
            startActivity(i);
        }
    }
    class MyAsyncTask extends AsyncTask<String, Integer, String> {
        String message ="";//反馈的消息

        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection connection = null;
            StringBuilder response = new StringBuilder();
            try {
                URL url = new URL(params[0]); // 声明一个URL,注意如果用百度首页实验，请使用https开头，否则获取不到返回报文
                connection = (HttpURLConnection) url.openConnection(); // 打开该URL连接
                connection.setRequestMethod("GET"); // 设置请求方法，“POST或GET”，我们这里用GET，在说到POST的时候再用POST
                connection.setConnectTimeout(80000); // 设置连接建立的超时时间
                connection.setReadTimeout(80000); // 设置网络报文收发超时时间
                InputStream in = connection.getInputStream();  // 通过连接的输入流获取下发报文，然后就是Java的流处理
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String line;
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return response.toString(); // 这里返回的结果就作为onPostExecute方法的入参

        }

        @Override
        protected void onPostExecute(String s) {
            message = s;
            Toast.makeText(RegisterActivity.this,message,Toast.LENGTH_SHORT).show();
        }
    }
}
